package web.admin.user;

import domain.User;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.admin.AdminFacade;
import service.facade.admin.ShopAdminService;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Provides settings of particular user
 */
public final class UserPanel extends Panel {

    @SpringBean
    private AdminFacade adminFacade;
    private final IModel<User> userModel;
    @SpringBean
    private ShopAdminService adminService;
    private static Logger logger = Logger.getLogger(UserPanel.class.getName());

    public UserPanel(String id, final Long userId, final UserAdminPanel parent) {
        super(id);
        setOutputMarkupId(true);
        userModel = new LoadableDetachableModel<User>() {

            @Override
            protected User load() {
                return adminFacade.findUserById(userId);
            }
        };
        add(new Form<User>("form", new CompoundPropertyModel<User>(userModel)) {

            {

                add(new CheckBox("enabled").setEnabled(false));
                add(new TextField("login"));
                add(new TextField("firstName"));
                add(new TextField("lastName"));

                add(new TextField("tradingName"));
                add(new TextField("tradingName2"));
                add(new TextField("companyID"));
                add(new TextField("taxID"));
                add(new TextField("bankNumber"));
                add(new TextField("companyEmail"));
                add(new TextField("phone"));
                add(new TextField("fax"));

                add(new TextField("street"));
                add(new TextField("municipality"));
                add(new TextField("postcode"));
                add(new TextField("country"));
            }

            @Override
            protected void onSubmit() {
                try {
                    adminFacade.updateUser(userModel.getObject());
                    info(getLocalizer().getString("saved", this));

                } catch (Exception e) {
                    e.printStackTrace();
                    error(getLocalizer().getString("system.error", this));
                }
            }
        });

        add(new Form("actions") {

            {
                Button delete = new Button("delete") {

                    @Override
                    public void onSubmit() {
                        try {

                            parent.detachUsersModel();
                            adminFacade.deleteUser(userModel.getObject());
                            parent.replacePanelToEmpty();
                            getSession().info(getLocalizer().getString("deleted", parent));
                        } catch (Exception e) {
                            logger.log(Level.SEVERE, null, e);
                            getSession().error(getLocalizer().getString("system.error", parent));

                        }

                    }
                };
                delete.setDefaultFormProcessing(false);
                Button deActivate = new Button("deActivate") {

                    @Override
                    public void onSubmit() {
                        try {
                            if (!adminService.deactivateAccount(userModel.getObject())) {
                                info(userModel.getObject().getLogin() + " " +
                                        getLocalizer().getString("user.success.wasDeactivated", this));
                            } else {
                                error(getLocalizer().getString("user.fail.wasDeactivated", this) + " " +
                                        userModel.getObject().getLogin());
                            }
                        } catch (Exception e) {
                            logger.log(Level.SEVERE, null, e);
                            error(getLocalizer().getString("system.error", this));

                        }


                        this.getParent().get("activate").setVisible(true);
                        this.setVisible(false);
                    }
                };
                Button activate = new Button("activate") {

                    @Override
                    public void onSubmit() {
                        try {
                            adminService.activateAccount(userModel.getObject());
                            info((userModel.getObject()).getLogin() + " " +
                                    getLocalizer().getString("user.success.wasActivated", this));
                            this.getParent().get("deActivate").setVisible(true);
                            this.setVisible(false);
                        } catch (Exception ex) {
                            Logger.getLogger(UserPanel.class.getName()).log(Level.SEVERE, "ActivateAccount: " + userModel.getObject().getLogin(), ex);
                            error(getLocalizer().getString("user.fail.wasActivated", this) + " " +
                                    userModel.getObject().getLogin());
                        }
                    }
                };
                add(delete);
                add(activate.setVisible(!userModel.getObject().isEnabled()));
                add(deActivate.setVisible(userModel.getObject().isEnabled()));
            }
        });

    }


}
